package cn.ulc.socket.admin.service.setInfo.impl;

import cn.ulc.common.exception.PlatformException;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.setInfo.SetInfoDTO;
import cn.ulc.socket.admin.model.param.setInfo.SetInfoSaveParam;
import cn.ulc.socket.admin.service.setInfo.SetInfoServ;
import cn.ulc.store.dao.SetInfoDao;
import cn.ulc.store.entity.SetInfo;
import cn.ulc.store.entity.staff.Staff;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class SetInfoServImpl extends BaseService implements SetInfoServ {
    SetInfoDao setInfoDao;

    @Override
    public Boolean update(Long id, SetInfoSaveParam param) {
        SetInfo setInfo = setInfoDao.findOne(id);
        if(setInfo==null) throw new PlatformException("获取配置信息失败!");
        setBaseInfo(setInfo);
        setInfo.setDeduct(param.getDeduct());
        setInfo.setDeductWater(param.getDeductWater());
        setInfo.setDepositAmount(param.getDepositAmount());
        setInfo.setMemberAmount(param.getMemberAmount());
        setInfo.setBeginPrice(param.getBeginPrice());
        setInfo.setDeliverPrice(param.getDeliverPrice());
        setInfo.setRemark(param.getRemark());
        setInfo.setFullPrice(param.getFullPrice());
        setInfoDao.save(setInfo);
        return true;
    }

    @Override
    public Boolean save(SetInfoSaveParam param) {
        Staff loginUser = getCurrentUser();
        if(loginUser==null) throw new PlatformException("请重新登录!");
        SetInfo setInfo = new SetInfo();
        SetInfo check = setInfoDao.findByCondition(loginUser.getCompanyId());
        if(check!=null) setInfo = check;
        setBaseInfo(setInfo);
        setInfo.setCompanyId(loginUser.getCompanyId());
        setInfo.setDeduct(param.getDeduct());
        setInfo.setDeductWater(param.getDeductWater());
        setInfo.setDepositAmount(param.getDepositAmount());
        setInfo.setMemberAmount(param.getMemberAmount());
        setInfo.setBeginPrice(param.getBeginPrice());
        setInfo.setDeliverPrice(param.getDeliverPrice());
        setInfo.setRemark(param.getRemark());
        setInfo.setFullPrice(param.getFullPrice());
        setInfoDao.save(setInfo);
        return true;
    }

    @Override
    public SetInfoDTO findDetail() {
        Staff loginUser = getCurrentUser();
        if(loginUser==null) throw new PlatformException("请重新登录!");
        SetInfo setInfo = setInfoDao.findByCondition(loginUser.getCompanyId());
//        if(setInfo==null) throw new PlatformException("获取配置信息失败!");
        return new SetInfoDTO(setInfo);
    }
}
